//
//  RealAuthenticationVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/1.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "RealAuthenticationVC.h"
#import "RealComplayAuthenticationTabVC.h"
#import "WriteRequirementTabVC.h"
#import "ReleaseServiceTabVC.h"
#import "MyShopVC.h"
#import "MyMessageTabVC.h"
@interface RealAuthenticationVC ()
@property (weak, nonatomic) IBOutlet UILabel *titleL;
@property (weak, nonatomic) IBOutlet UIButton *personAuthenticationBtn;
@property (weak, nonatomic) IBOutlet UIButton *complanyAuthBtn;
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,assign) NSInteger tag;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property (weak, nonatomic) IBOutlet MCButton *lookAuthBtn;
@property (weak, nonatomic) IBOutlet MCButton *pushBtn;
@property(nonatomic,strong) UIImageView *upIV;
@property(nonatomic,strong) UIImageView *downIV;
@property (weak, nonatomic) IBOutlet UIView *authView;
@property (weak, nonatomic) IBOutlet UILabel *authL;
@property (weak, nonatomic) IBOutlet UILabel *authContentL;
@property (weak, nonatomic) IBOutlet UIImageView *authIV;
@property(nonatomic,strong) NSMutableArray *sandboxMuta;
@property(nonatomic,strong) WriteRequirementTabVC *writeDemandVC;
@property(nonatomic,strong) ReleaseServiceTabVC *writeServiceVC;
@property(nonatomic,strong) MyShopVC *myShopVC;
@property(nonatomic,strong) MyMessageTabVC *myMessageVC;
@property(nonatomic,assign) NSInteger date;
@end

@implementation RealAuthenticationVC
-(NSMutableArray *)sandboxMuta
{
    if (!_sandboxMuta) {
        NSArray *pathFile = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *homeSearchPreference = [pathFile[0] stringByAppendingPathComponent:@"auth.plist"];
        NSArray *temp = [NSArray arrayWithContentsOfFile:homeSearchPreference];
        XLog(@"temp.count:%ld",temp.count);
        if (temp.count) {
            _sandboxMuta = [NSMutableArray arrayWithArray:temp];
        }else {
            _sandboxMuta = [NSMutableArray array];
        }
    }
    return _sandboxMuta;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleL.textColor = kLabel153Color;
    self.manager = [AFHTTPSessionManager manager];
    self.writeDemandVC = [[WriteRequirementTabVC alloc]init];
    self.writeServiceVC = [[ReleaseServiceTabVC alloc]init];
    self.myShopVC = [[MyShopVC alloc]init];
    
    self.myMessageVC = [[MyMessageTabVC alloc]init];
    [self.personAuthenticationBtn setTitleColor:[UIColor colorWithRed:151/255.0 green:193/255.0 blue:91/255.0 alpha:1] forState:UIControlStateNormal];
    self.personAuthenticationBtn.layer.cornerRadius = 8;
    self.personAuthenticationBtn.layer.borderColor  = [UIColor colorWithRed:151/255.0 green:193/255.0 blue:91/255.0 alpha:1].CGColor;
    self.personAuthenticationBtn.layer.borderWidth  = 1;
    [self.complanyAuthBtn setTitleColor:kOrangeBg forState:UIControlStateNormal];
    self.complanyAuthBtn.layer.cornerRadius = 8;
    self.complanyAuthBtn.layer.borderColor  = kOrangeBg.CGColor;
    self.complanyAuthBtn.layer.borderWidth  = 1;
    self.lookAuthBtn.layer.masksToBounds = YES;
    self.lookAuthBtn.layer.cornerRadius = 6;
    self.pushBtn.layer.masksToBounds = YES;
    self.pushBtn.layer.cornerRadius = 6;
    self.lookAuthBtn.buttonStyle = imageCustomeLeft;
    self.pushBtn.buttonStyle = imageCustomeLeft;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(simplecertification:) name:@"simplecertification" object:nil];
    if ([[UserDefaultsHelper readType]integerValue]!=0) {
        [self checkSimplecertification];
    }
   
}
- (IBAction)personAuthBtn:(UIButton *)sender {
    self.tag = sender.tag;
//    [self checkSimplecertification];
    RealComplayAuthenticationTabVC *realPersonVC = [[RealComplayAuthenticationTabVC alloc]init];
    realPersonVC.realPerson = YES;
    [self.navigationController pushViewController:realPersonVC animated:YES];
    
}
- (IBAction)complanyAuthBtn:(UIButton *)sender {
    self.tag = sender.tag;
//    [self checkSimplecertification];
    RealComplayAuthenticationTabVC *realPersonVC = [[RealComplayAuthenticationTabVC alloc]init];
    realPersonVC.realPerson = NO;
    [self.navigationController pushViewController:realPersonVC animated:YES];
}
- (IBAction)lookAuthBtn:(MCButton *)sender {
    if ([[sender currentTitle]isEqualToString:[NSBundle bundleLocalizedStringForKey:@"releaseService"]]) {
        [self.navigationController pushViewController:self.writeServiceVC animated:YES];
    }else if ([[sender currentTitle]isEqualToString:[NSBundle bundleLocalizedStringForKey:@"authCompany2ButtonUp"]]) {
        RealComplayAuthenticationTabVC *realPersonVC = [[RealComplayAuthenticationTabVC alloc]init];
        if ([[UserDefaultsHelper readType]integerValue] ==1) {
            realPersonVC.realPerson = YES;
            realPersonVC.status = 1;
        }else {
            realPersonVC.realPerson = NO;
            realPersonVC.status = 2;
        }
        realPersonVC.showMsg = YES;
        [self.navigationController pushViewController:realPersonVC animated:YES];
    }else if ([[sender currentTitle]isEqualToString:[NSBundle bundleLocalizedStringForKey:@"authCompany2ButtonUp"]]) {
        RealComplayAuthenticationTabVC *realPersonVC = [[RealComplayAuthenticationTabVC alloc]init];
        realPersonVC.realPerson = YES;
        realPersonVC.status = 1;
        realPersonVC.showMsg = YES;
        [self.navigationController pushViewController:realPersonVC animated:YES];
    }else if ([[sender currentTitle]isEqualToString:[NSBundle bundleLocalizedStringForKey:@"authCompanyDefeatButtonUp"]]) {
        [self.navigationController pushViewController:self.myMessageVC animated:YES];
    }
}
- (IBAction)pushBtn:(MCButton *)sender {
    if ([[sender currentTitle]isEqualToString:[NSBundle bundleLocalizedStringForKey:@"myShop"]]) {
        [self.navigationController pushViewController:self.myShopVC animated:YES];
    }else if ([[sender currentTitle]isEqualToString:[NSBundle bundleLocalizedStringForKey:@"authPersonButtonDown"]]) {
        RealComplayAuthenticationTabVC *realPersonVC = [[RealComplayAuthenticationTabVC alloc]init];
        realPersonVC.realPerson = NO;
        realPersonVC.showMsg = NO;
        [self.navigationController pushViewController:realPersonVC animated:YES];
    }else if ([[sender currentTitle]isEqualToString:[NSBundle bundleLocalizedStringForKey:@"authCompanyDefeatButtonDown"]]) {
        RealComplayAuthenticationTabVC *realPersonVC = [[RealComplayAuthenticationTabVC alloc]init];
        if (self.date == 4) {
            realPersonVC.realPerson = NO;
        }else {
            realPersonVC.realPerson = YES;
        }
        realPersonVC.showMsg = NO;
        [self.navigationController pushViewController:realPersonVC animated:YES];
    }else if ([[sender currentTitle]isEqualToString:[NSBundle bundleLocalizedStringForKey:@"releaseService"]]) {
        [self.navigationController pushViewController:self.writeServiceVC animated:YES];
    }
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark-发送---实名认证
-(void)checkSimplecertification
{
    NSArray *pathFile = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *complain = @"";
    complain = [pathFile[0] stringByAppendingPathComponent:@"auth.plist"];
    self.authView.hidden = NO;
    [self.lookAuthBtn addSubview:self.upIV];
    [self.pushBtn addSubview:self.downIV];
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    self.urlString = [NSString stringWithFormat:@"%@/simplecertification/Authenticationstatus",kUrl];
    self.dict = @{
                  @"uid":[UserDefaultsHelper readUid]
                  };
    [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *message = [responseObject objectForKey:@"message"];
        NSInteger date = [[responseObject objectForKey:@"date"]integerValue];
        XLog(@"responseObject:%@",responseObject);
        if (date == 1) {
        }else if (date == 2) {//企业认证成功
            self.title = @"企业实名认证";
            self.authIV.image = [UIImage imageNamed:@"b-ertificationcomplete"];
            self.authL.text = [NSBundle bundleLocalizedStringForKey:@"authCompanyTitle"];
            self.authContentL.text = [NSBundle bundleLocalizedStringForKey:@"authCompanyContent"];
            [self.lookAuthBtn setTitle:[NSBundle bundleLocalizedStringForKey:@"authCompany2ButtonUp"] forState:UIControlStateNormal];
            [self.pushBtn setTitle:[NSBundle bundleLocalizedStringForKey:@"releaseService"] forState:UIControlStateNormal];
            self.lookAuthBtn.backgroundColor = kOrangeBg;
            self.pushBtn.backgroundColor = kAuthColor;
            [self.lookAuthBtn setImage:[UIImage imageNamed:@"authenticated"] forState:UIControlStateNormal];
            [self.pushBtn setImage:[UIImage imageNamed:@"issue"] forState:UIControlStateNormal];
        }else if (date == 3||date == 6) {//企业,个人审核中
            if (date == 3) {
                self.title = @"企业实名认证";
            }else{
                self.title = @"个人实名认证";
            }
            self.authIV.image = [UIImage imageNamed:@"submit"];
            self.authL.text = [NSBundle bundleLocalizedStringForKey:@"authTitle"];
            self.authContentL.text = [NSBundle bundleLocalizedStringForKey:@"authContnet"];
            self.lookAuthBtn.hidden = YES;
            self.pushBtn.hidden = YES;
        }else if (date == 4) {//企业认证失败
            self.title = @"企业实名认证";
            self.date =  4;
            self.authIV.image = [UIImage imageNamed:@"d-businesscertification"];
            self.authL.text = [NSBundle bundleLocalizedStringForKey:@"authDefeat"];
            self.authContentL.text = [NSBundle bundleLocalizedStringForKey:@"authCompanyDefeat"];
            [self.lookAuthBtn setTitle:[NSBundle bundleLocalizedStringForKey:@"authCompanyDefeatButtonUp"] forState:UIControlStateNormal];
            [self.pushBtn setTitle:[NSBundle bundleLocalizedStringForKey:@"authCompanyDefeatButtonDown"] forState:UIControlStateNormal];
            self.lookAuthBtn.backgroundColor = kOrangeBg;
            self.pushBtn.backgroundColor = kAuthColor;
            [self.lookAuthBtn setImage:[UIImage imageNamed:@"issue"] forState:UIControlStateNormal];
            [self.pushBtn setImage:[UIImage imageNamed:@"store"] forState:UIControlStateNormal];
        }else if (date == 5) {//个人认证成功
            self.title = @"个人实名认证";
            self.authL.text = [NSBundle bundleLocalizedStringForKey:@"authSucess"];
            if (!self.sandboxMuta.count) {
                self.authIV.image = [UIImage imageNamed:@"s-personalcertification"];
                self.authContentL.text = [NSBundle bundleLocalizedStringForKey:@"authPersonUpdate"];
                [self.lookAuthBtn setTitle:[NSBundle bundleLocalizedStringForKey:@"releaseService"] forState:UIControlStateNormal];
                [self.pushBtn setTitle:[NSBundle bundleLocalizedStringForKey:@"myShop"] forState:UIControlStateNormal];
                self.lookAuthBtn.backgroundColor = kAuthColor;
                self.pushBtn.backgroundColor = kOrangeBg;
                [self.lookAuthBtn setImage:[UIImage imageNamed:@"issue"] forState:UIControlStateNormal];
                [self.pushBtn setImage:[UIImage imageNamed:@"store"] forState:UIControlStateNormal];
                [self.sandboxMuta addObject:@"1"];
                [self.sandboxMuta writeToFile:complain atomically:YES];
                XLog(@"self.sandboxMuta:%@",self.sandboxMuta);
            }else {
                self.authIV.image = [UIImage imageNamed:@"p-ertificationcomplete"];
                self.authContentL.text = [NSBundle bundleLocalizedStringForKey:@"authPersonContent2"];
                [self.lookAuthBtn setTitle:[NSBundle bundleLocalizedStringForKey:@"authCompany2ButtonUp"] forState:UIControlStateNormal];
                [self.pushBtn setTitle:[NSBundle bundleLocalizedStringForKey:@"authPersonButtonDown"] forState:UIControlStateNormal];
                self.lookAuthBtn.backgroundColor = kOrangeBg;
                self.pushBtn.backgroundColor = kOrangeBg;
                [self.lookAuthBtn setImage:[UIImage imageNamed:@"authenticated"] forState:UIControlStateNormal];
                [self.pushBtn setImage:[UIImage imageNamed:@"business"] forState:UIControlStateNormal];
            }
        }else if (date == 7) {//个人认证失败
            self.title = @"个人实名认证";
            self.date =  7;
            self.authIV.image = [UIImage imageNamed:@"d-personalcertification"];
            self.authL.text = [NSBundle bundleLocalizedStringForKey:@"authDefeat"];
            self.authContentL.text = [NSBundle bundleLocalizedStringForKey:@"authPersonDefeat"];
            [self.lookAuthBtn setTitle:[NSBundle bundleLocalizedStringForKey:@"authCompanyDefeatButtonUp"] forState:UIControlStateNormal];
            [self.pushBtn setTitle:[NSBundle bundleLocalizedStringForKey:@"authCompanyDefeatButtonDown"] forState:UIControlStateNormal];
            self.lookAuthBtn.backgroundColor = kOrangeBg;
            self.pushBtn.backgroundColor = kAuthColor;
            [self.lookAuthBtn setImage:[UIImage imageNamed:@"information"] forState:UIControlStateNormal];
            [self.pushBtn setImage:[UIImage imageNamed:@"authentication"] forState:UIControlStateNormal];
        }else if (date == 9) {//个人升级企业
            self.title = @"企业实名认证";
            self.authIV.image = [UIImage imageNamed:@"s-businesscertification"];
            self.authL.text = [NSBundle bundleLocalizedStringForKey:@"authSucess"];
            self.authContentL.text = [NSBundle bundleLocalizedStringForKey:@"authCompanyUpdate"];
            [self.lookAuthBtn setTitle:[NSBundle bundleLocalizedStringForKey:@"releaseService"] forState:UIControlStateNormal];
            [self.pushBtn setTitle:[NSBundle bundleLocalizedStringForKey:@"myShop"] forState:UIControlStateNormal];
            self.lookAuthBtn.backgroundColor = kAuthColor;
            self.pushBtn.backgroundColor = kOrangeBg;
            [self.lookAuthBtn setImage:[UIImage imageNamed:@"issue"] forState:UIControlStateNormal];
            [self.pushBtn setImage:[UIImage imageNamed:@"store"] forState:UIControlStateNormal];
        }else if (date == 10) {//直接认证个人
            self.title = @"个人实名认证";
            self.authIV.image = [UIImage imageNamed:@"b-ertificationcomplete"];
            self.authL.text = [NSBundle bundleLocalizedStringForKey:@"authCompanyTitle"];
            self.authContentL.text = [NSBundle bundleLocalizedStringForKey:@"authCompanyContent"];
            [self.lookAuthBtn setTitle:[NSBundle bundleLocalizedStringForKey:@"authCompany2ButtonUp"] forState:UIControlStateNormal];
            [self.pushBtn setTitle:[NSBundle bundleLocalizedStringForKey:@"releaseService"] forState:UIControlStateNormal];
            [self.lookAuthBtn setImage:[UIImage imageNamed:@"issue"] forState:UIControlStateNormal];
            [self.pushBtn setImage:[UIImage imageNamed:@"store"] forState:UIControlStateNormal];
        }
        
        [WSProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [WSProgressHUD dismiss];
    }];
}
#pragma mark-代理:检测实名认证
-(void)simplecertification:(NSNotification *)info
{
    
}

@end
